package com.blinddate.controller;

import com.blinddate.pojo.dto.OrdersPaymentDTO;
import com.blinddate.pojo.entity.Orders;
import com.blinddate.pojo.result.Result;
import com.blinddate.pojo.vo.OrderPaymentVO;
import com.blinddate.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@RequestMapping("/pay")
@Slf4j
@Api(tags = "支付功能功能")
public class PayController {

    @Autowired
    private OrdersService ordersService;


    @PostMapping("/place_order")
    @ApiOperation("下单")
    public Result<Orders> placeOrder(@RequestBody Orders orders){
        orders.setOutTradeNo(UUID.randomUUID().toString());
        orders.setStatus(0);
        orders.setOrderTime(LocalDateTime.now());
        boolean save = ordersService.save(orders);
        if (save){
            return Result.success(orders);
        }
        return Result.error("订单生成失败");
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }
}
